package server.config.shiro;


import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import server.dao.FrmDataResourcesDao;
import server.model.entity.FrmDataResources;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class AutoShiro extends ShiroFilterFactoryBean {

    /**权限service*/

    /**
     * 初始化设置过滤链
     */
    public AutoShiro(FrmDataResourcesDao fspDataRecordDao) {
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        filterChainDefinitionMap.put("/css/**", "anon");
        filterChainDefinitionMap.put("/login", "authc");
        filterChainDefinitionMap.put("/img/**", "anon");
        filterChainDefinitionMap.put("/js/**", "anon");
        filterChainDefinitionMap.put("/loginout", "loginout");


        //vsp授权
        List<FrmDataResources> vspList = fspDataRecordDao.findByType(FrmDataResources.Type.VIEW);
        for (FrmDataResources vp : vspList) {
            if (vp.getStatus()) {
                filterChainDefinitionMap.put(vp.getUri(), vp.getRule());
            } else {
                filterChainDefinitionMap.put(vp.getUri(), "authc,kickout,perms[band]");
            }
        }
        filterChainDefinitionMap.put("/view/index", "authc,kickout,perms[index]");

        //dsp授权
        List<FrmDataResources> dspList = fspDataRecordDao.findByType(FrmDataResources.Type.API);
        for (FrmDataResources vp : dspList) {
            if (vp.getStatus()) {
                filterChainDefinitionMap.put(vp.getUri(), vp.getRule());
            } else {
                filterChainDefinitionMap.put(vp.getUri(), "authc,kickout,perms[band]");
            }
        }
        super.setFilterChainDefinitionMap(filterChainDefinitionMap);
    }
}
